﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Model.Content;

namespace Cn.Cellula.Website.Ws {
    public partial class _Vote : PageSupport {
        IVoteBiz biz = BizFactory.Get<IVoteBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            String act = Request.QueryString["act"];
            String json = "";
            if (String.IsNullOrEmpty(act) || act == "load") {
                try {
                    json = LoadVote2Json();
                } catch (Exception ex) {
                    log.Error(ex.Message);
                }
                Response.Clear();
                Response.Write(json);
                Response.End();
            } else {
                Response.Clear();
                Response.Write("null");
                Response.End();
            }
        }

        private String LoadVote2Json() {
            TVoteEO eo = biz.Load<TVoteEO>(Request.QueryString["id"]);
            DateTime start = (eo.StartDate == null) ? DateTime.Now.AddYears(1) : Convert.ToDateTime(eo.StartDate);
            StringBuilder sb = new StringBuilder();
            //尚未开始
            if (start.CompareTo(DateTime.Now) > 0) {
                return "0";
            } else {
                if (eo.EndDate != null) {
                    DateTime end = Convert.ToDateTime(eo.EndDate);
                    if (end.CompareTo(DateTime.Now) < 0) {
                        return "9";
                    }
                }
            }
            sb.Append("{");
            sb.Append("\"VoteKey\":\""+eo.VoteKey+"\"");
            sb.Append(",\"VoteTitle\":\"" + eo.VoteTitle + "\"");
            sb.Append(",\"VoteDesc\":\"" + eo.VoteDesc + "\"");
            sb.Append(",\"IsMultiSelect\":\"" + eo.IsMultiSelect + "\"");
            sb.Append(",\"Items\":[");
            IList<TVoteItemEO> list = biz.LoadItemsByVote(eo.VoteKey);
            if (list != null) {
                for (int i = 0; i < list.Count; i++) {
                    TVoteItemEO item = list[i];
                    if (i > 0) sb.Append(",");
                    sb.Append("{");
                    sb.Append("\"ItemKey\":\"" + item.ItemKey + "\"");
                    sb.Append(",\"ItemContent\":\"" + item.ItemContent + "\"");
                    sb.Append(",\"Score\":\"" + item.Score + "\"");
                    sb.Append("}");
                }
            }
            sb.Append("]");
            sb.Append("}");
            return sb.ToString();
        }
    }
}
